package org.reenscape.skill;
import org.reenscape.updater.*;
import org.reenscape.model.*;
import org.reenscape.lib.*;
public class Melee {

					public void NPC(int slot, PacketParser p3){
						if(!(slot <= NpcUpdater.maxNPCs && slot >= 1)) return;
						if(NpcUpdater.npcs[slot] == null) return;
						int X = NpcUpdater.npcs[slot].absX;
						int Y = NpcUpdater.npcs[slot].absY;
						int Index = p3.inStream.readSignedWordBigEndianA();
						int HP = NpcUpdater.npcs[slot].HP;
						boolean Dead = NpcUpdater.npcs[slot].IsDead;
						boolean ThisDead = p3.IsDead;
						
						int Hit = 0;
						double XP = 0;
						int Delay = GetWeaponDelay(p3);
						int HitMax = GetHitMax(p3);
						int Distance = p3.distanceToPoint(X, Y);
						
						if(Distance < 2){
							if(!ThisDead){
								NpcUpdater.npcs[slot].StartKilling = p3.playerId;
								NpcUpdater.npcs[slot].RandomWalk = false;
								NpcUpdater.npcs[slot].IsUnderAttack = true;		
								int k57 = p3.Delay;
								if((k57 == 0) ||(k57 >= 2 && k57 <= 4)){ //0 is what resets this counter in the first place, made a mistake with the old try :)
									if(!Dead){
										p3.faceNPC(slot);
										p3.setAnimation(GetAnimation(p3));
										NpcUpdater.npcs[slot].RotateToPoint(p3.absX, p3.absY);
										p3.Delay += Delay;
										Hit = Library.random.random(HitMax);
										if((HP - Hit) < 0){
											Hit = HP;
										}
										Hit = (int) Hit;
										NpcUpdater.npcs[slot].hitDiff = Hit;
										NpcUpdater.npcs[slot].Killing[p3.playerId] += Hit;
										NpcUpdater.npcs[slot].updateRequired = true;
										NpcUpdater.npcs[slot].hitUpdateRequired = true;
										
										int F = p3.FightType;
										XP = (double) Hit * 4;
										if(XP > 0){
											if(F == 1){
												p3.addSkillXP(XP, Skill.ATTACK);
											}
											if(F == 2){
												p3.addSkillXP(XP, Skill.STRENGTH);
											}
											if(F == 3){
												p3.addSkillXP(XP - 1, Skill.ATTACK);
												p3.addSkillXP(XP - 1, Skill.STRENGTH);
												p3.addSkillXP(XP - 1, Skill.DEFENCE);
											}
											if(F == 4){
												p3.addSkillXP(XP, Skill.DEFENCE);
											}
											if(Hit > 0)
												p3.addSkillXP(Hit, Skill.HITPOINTS);
										}
									} else {
										p3.resetAnimation();
										p3.MeleeNpcSlot = 0;
										p3.Delay = -1;
									}
								}
							} else {
								p3.resetAnimation();
								p3.MeleeNpcSlot = 0;
								p3.Delay = -1;
								NpcUpdater.npcs[slot].IsUnderAttack = false;
								NpcUpdater.npcs[slot].RandomWalk = true;
							}
						}
					}
					
					public void Player(int slot, PacketParser p3){
						if(!(slot <= ClientUpdater.maxPlayers && slot >= 1)) return;
						if(ClientUpdater.players[slot] == null) return;
						int X = ClientUpdater.players[slot].absX;
						int Y = ClientUpdater.players[slot].absY;
						int Index = p3.inStream.readSignedWordBigEndianA();
						int HP = ClientUpdater.players[slot].playerLevel[3];
						boolean Dead = ClientUpdater.players[slot].IsDead;
						boolean ThisDead = p3.IsDead;
						PacketParser p4 = (PacketParser) ClientUpdater.players[slot];
						if(p4 == null) return;
						if(!(p3.absY >= 3520 && p4.absY >= 3520)){
							p3.MeleePlayerSlot = 0;
							p3.sendMessage("This player is currently not in the wild.");
							return;
						}
						
						int Hit = 0;
						double XP = 0;
						int Delay = GetWeaponDelay(p3);
						int HitMax = GetHitMax(p3);
						int Distance = p3.distanceToPoint(X, Y);
						
						if(Distance < 2){
							if(!ThisDead){
								if(p3.Delay < 1){
									if(!Dead){
										p3.RotateToPoint(X, Y);
										p4.MeleePlayerSlot = p3.playerId;
										p3.setAnimation(GetAnimation(p3));
										p4.RotateToPoint(p3.absX, p3.absY);
										p3.Delay += Delay;
										Hit = Library.random.random(HitMax);
										if((HP - Hit) < 0){
											Hit = HP;
										}
										Hit = (int) Hit;
										p4.hitDiff = Hit;
										if(p4.playerEquipment[p4.playerShield] == -1){
											p4.setPlrAnimation(p4.GetPlrBlockAnim(p4.playerEquipment[p4.playerWeapon]));
										} else {
											p4.setPlrAnimation(p4.GetPlrShieldBlockAnim(p4.playerEquipment[p4.playerShield]));
										}
										p4.updateRequired = true;
										p4.hitUpdateRequired = true;
										
										int F = p3.FightType;
										XP = (double) Hit * 4;
										if(XP > 0){
											if(F == 1){
												p3.addSkillXP(XP, Skill.ATTACK);
											}
											if(F == 2){
												p3.addSkillXP(XP, Skill.STRENGTH);
											}
											if(F == 3){
												p3.addSkillXP(XP - 1, Skill.ATTACK);
												p3.addSkillXP(XP - 1, Skill.STRENGTH);
												p3.addSkillXP(XP - 1, Skill.DEFENCE);
											}
											if(F == 4){
												p3.addSkillXP(XP, Skill.DEFENCE);
											}
											if(Hit > 0)
												p3.addSkillXP(Hit, Skill.HITPOINTS);
										}
									} else {
										p3.resetAnimation();
										p3.MeleePlayerSlot = 0;
									}
								}
							} else {
								p3.resetAnimation();
								p3.MeleePlayerSlot = 0;
							}
						}
					}
					
					public int GetHitMax(PacketParser p3){
						//LAST UPDATED JULY 16TH 2007
						double MaxHit = 0;
						int StrengthBonus = p3.playerBonus[10];
						int Strength = p3.playerLevel[p3.playerStrength];
						
						MaxHit = (((Strength)*(1))+(1))*(((+GetPow(p3))*0.00175)+0.1)+1.05;
						int MaxHitUnRaw = (int) java.lang.Math.ceil(MaxHit);
						if(MaxHitUnRaw > MaxHit) MaxHitUnRaw -= 1;
						int StrengthHit = (StrengthBonus / 8);
						if(StrengthHit > 0) MaxHit += StrengthHit;
						return MaxHitUnRaw;
					}
					
					public int GetWeaponDelay(PacketParser p3){
						return 8;
					}
					public int GetAnimation(PacketParser p3){
						int a = p3.FightType;
						int b = p3.playerEquipment[p3.playerWeapon];
			
						if(b == 4151) return 1658; // Abyssal whip
						if(b == -1){ // Unarmed
							if(a == 2) return 423;
							else return 422;
						}
						if(b == 1419){ // Scythe
							if(a == 1) return 408;
							else return 412;
						}
						if(b == 3204 || b == 7809){ //Dragon halberd/Anger spear
							if(a == 1) return 400;
							if(a == 2) return 440;
							if(a == 3) return 400;
						}
						if(b == 1277 || b == 1279 || b == 1281 || b == 1283 || b == 1285 || b == 1287){ // Short swords
							if(a == 3) return 412;
							else return 451;
						}
						if(b == 1203 || b == 1205 || b == 1207 || b == 1209 || b == 1211 || b == 1213 || b == 1217){ //Daggers bronze-rune minus dragon (seperate)
							if(a == 1 || a == 4) return 412;
							else return 451;
						}
						if(b == 4153) return 1665; //Granite Maul
						if(b == 1377) return 1833; //Dragon battle axe
						if(b == 4718) return 2067; //Dharok's axe
						if(b == 4726) return 2080; //Guthan's spear
						if(b == 4747) return 2068; //Torag's hammers
						if(b == 4755) return 2062; //Verac's flail
						if(b == 4734) return 2075; //Karil's crossbow
						if(b == 7158) return 407; // Dragon two hand sword
						if(b == 1434) return 1060; // Dragon mace
						if(b == 1291 || b == 1293 || b == 1295 || b == 1297 || b == 1299 || b == 1301 || b == 1303 || b == 1305) return 451; //Long swords
						if(b == 1321 || b == 1323 || b == 1325 || b == 1327 || b == 1329 || b == 1331 || b == 1333 || b == 4587 || b == 6611) return 451; //Scimitars
						if(b == 1215 || b == 1231 || b == 5680 || b == 5698) return 0x426; //Dragon daggers
						if(b == 1307 || b == 1309 || b == 1311 || b == 1313 || b == 1315 || b == 1317 || b == 1319) return 406; //Two hand swords
						else return 0x326;
					}
					
					public int GetPow(PacketParser p3){
						int aa = p3.playerEquipment[p3.playerWeapon];
						
						if(aa == -1) return 5;
						
						if(aa == 1205) return 5;
						if(aa == 1203) return 6;
						if(aa == 1207) return 9;
						if(aa == 1217) return 9;
						if(aa == 1209) return 12;
						if(aa == 1211) return 16;
						if(aa == 1213) return 26;
						if(aa == 4718) return 105;
						
						if(aa == 1277) return 7;
						if(aa == 1279) return 9;
						if(aa == 1281) return 14;
						if(aa == 1283) return 14;
						if(aa == 1285) return 19;
						if(aa == 1287) return 26;
						if(aa == 1289) return 41;
						
						if(aa == 1291) return 8;
						if(aa == 1293) return 11;
						if(aa == 1295) return 17;
						if(aa == 1297) return 17;
						if(aa == 1299) return 23;
						if(aa == 1301) return 32;
						if(aa == 1303) return 50;
						if(aa == 1305) return 72;

						if(aa == 4151) return 80;
						
						if(aa == 1307) return 11;
						if(aa == 1309) return 15;
						if(aa == 1311) return 23;
						if(aa == 1313) return 23;
						if(aa == 1315) return 32;
						if(aa == 1317) return 45;
						if(aa == 1319) return 71;
						
						if(aa == 667) return 11;
						if(aa == 2402) return 11;
						
						if(aa == 1321) return 7;
						if(aa == 1323) return 10;
						if(aa == 1325) return 15;
						if(aa == 1327) return 25;
						if(aa == 1329) return 21;
						if(aa == 1331) return 29;
						if(aa == 1333) return 45;
						if(aa == 4587) return 74;
						
						if(aa == 1351) return 6;
						if(aa == 1349) return 8;
						if(aa == 1353) return 12;
						if(aa == 1355) return 16;
						if(aa == 1357) return 21;
						if(aa == 1359) return 37;
						
						if(aa == 1375) return 10;
						if(aa == 1363) return 14;
						if(aa == 1365) return 21;
						if(aa == 1367) return 27;
						if(aa == 1369) return 30;
						if(aa == 1371) return 42;
						if(aa == 1373) return 65;
						if(aa == 1377) return 76;
						
						if(aa == 1422) return 5;
						if(aa == 1420) return 7;
						if(aa == 1424) return 9;
						if(aa == 1426) return 11;
						if(aa == 1428) return 13;
						if(aa == 1430) return 19;
						if(aa == 1432) return 39;
						if(aa == 1434) return 46;
						
						else {
							/*System.out.println("No weapon power set: "+aa+"!");*/ return 7;
						}
					}
}